package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.DosingMapper;
import com.indusfo.spc.mapper.UploadMapper;
import com.indusfo.spc.pojo.Dosing;
import com.indusfo.spc.pojo.Mat;
import com.indusfo.spc.pojo.Pro;
import com.indusfo.spc.service.DosingService;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class DosingServiceImpl implements DosingService {

    private  static  final Logger logger = LoggerFactory.getLogger(DosingServiceImpl.class);

    @Resource
    private DosingMapper dosingMapper;

    @Resource
    UploadMapper uploadMapper;

    /**
     * 编辑
     *
     * @param dosing
     * @return  JSONObject
     */
    @Override
    public JSONObject updateDosing(Dosing dosing) {
        try {
            if(dosing.getDosingId() == null) {
                throw new ParamsErrorException("请选择要编辑的叫料信息");
            }
            checkParam(dosing);
            // 调用存储过程
            int row = dosingMapper.updateByPrimaryKeySelective(dosing);
            if(row == 0) {
                throw new ModifyFailedException("数据编辑失败");
            }
            return JSONObject.oK("编辑成功");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /***
     * 查询（分页）
     *
     * @return com.indusfo.spc.vo.JSONObject
     */
    @Override
    public JSONObject selectAll(Dosing dosing) {
        List<Dosing> listDosing = null;
        try {
            Integer pagesize = dosing.getPagesize();
            Integer pageindex = dosing.getPageindex();
            if(pagesize != null && pageindex != null) {
                dosing.setIncept(pagesize*(pageindex - 1));
            }
            //查询全部
            listDosing = dosingMapper.selectAll(dosing);
            if (listDosing.isEmpty()) {
                return JSONObject.oK("没有查询到数据", listDosing, 0);
            }
            //查询分页总记录数
            int count = dosingMapper.countDosing(dosing); //返回查询到的总记录数
            return JSONObject.oK("查询成功", listDosing, count);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /***
     * 新增
     *
     * @param dosing
     * @return com.indusfo.spc.vo.JSONObject
     */
    @Override
    public JSONObject insertDosing(Dosing dosing) {
        try {
            if(dosing.getDosingId() != null) {
                throw new ParamsErrorException("新增时，不能填写id");
            }
            checkParam(dosing);
            int	row = dosingMapper.insertSelective(dosing);
            if(row == 0) {
                throw new ModifyFailedException("数据新增失败");
            }
            return JSONObject.oK("新增成功");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 1 启用  2 删除  3停用 
     *
     * @param dosingIds
     * @param dataState
     * @return com.indusfo.spc.vo.JSONObject
     */
    @Override
    public JSONObject definedDosing(Long[] dosingIds, Integer dataState) {
        try {
            if(dataState == null) {
                throw new ParamsErrorException("数据状态不能为空");
            }else if(dataState != 1 && dataState != 2 && dataState != 3) {
                throw new ParamsErrorException("数据状态错误");
            }
            if (dosingIds == null || dosingIds.length == 0) {
                throw new ParamsErrorException("叫料信息ID不能为空");
            }
            // 执行存储过程
            int row = dosingMapper.definedDosing(dosingIds, dataState);
            String msg = "";
            if (row == 0) {
                // 判断传入的数据状态参数lDataState,返回相应信息
                switch (dataState) {
                    case 1:
                        throw new ModifyFailedException("启用失败");
                    case 2:
                        throw new ModifyFailedException("删除失败");
                    case 3:
                        throw new ModifyFailedException("停用失败");
                    default:
                }
            } else {
                switch (dataState) {
                    case 1:
                        msg = "启用成功";break;
                    case 2:
                        msg = "删除成功";break;
                    case 3:
                        msg = "停用成功";break;
                    default:
                }
            }
            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public String checkExcelContent(Dosing dosing) {
        String productCode = dosing.getProductCode();
        String proName = dosing.getProName();
        String bomCode = dosing.getBomCode();
        if(StringUtils.isEmpty(productCode)){
            return "产品编码为空";
        }
        Map<String, Mat> productMap = uploadMapper.queryProduct();
        if(!productMap.containsKey(productCode)){
            return "产品编码填写不标准/不规范";
        }
        dosing.setProductId(productMap.get(productCode).getMatId());
        if(StringUtils.isEmpty(proName)){
            return "工序名称为空";
        }
        Map<String, Pro> proMap = uploadMapper.queryPro();
        if(!proMap.containsKey(proName)){
            return "工序名称填写不标准/不存在";
        }
        dosing.setProId(proMap.get(proName).getProId());
        if(StringUtils.isEmpty(bomCode)){
            return "物料编码为空";
        }
        Map<String, Mat> matMap = uploadMapper.queryMat();
        if(!matMap.containsKey(bomCode)){
            return "物料编码填写不标准/不存在";
        }
        dosing.setBomId(matMap.get(bomCode).getMatId());
        return null;
    }

    @Override
    public JSONObject updateListDosing(List<Dosing> dosingList) {
        uploadMapper.updateListDosing(dosingList);
        return null;
    }

    /**
     * 叫料信息校验
     * @param dosing
     */
    public void checkParam(Dosing dosing){
        if (dosing.getProductId() == null) {
            throw new ParamsErrorException("请选择产品");
        }
        if (dosing.getProId() == null) {
            throw new ParamsErrorException("请选择工序");
        }
        if (dosing.getBomId() == null) {
            throw new ParamsErrorException("请选择物料");
        }
        // 不能重复
        Integer CodeCounts = dosingMapper.queryDosing(dosing);
        if (CodeCounts !=0) {
            throw new ParamsErrorException("该叫料信息已存在");
        }
    }


}
